#include <dbg.h>

#include <cassert>

using namespace std;

class Solution {
public:
    bool canChange(string start, string target) {
        string startLR = removeUnderscore(start);
        string targetLR = removeUnderscore(target);
        if (startLR !=
            targetLR) {  // 去掉 _ 后的字符不匹配，L 和 R 无法调换位置
            return false;
        }

        int indexS = 0;  // 双指针
        int indexT = 0;
        for (; indexS < start.size(); ++indexS) {
            if (start[indexS] == '_') {
                continue;
            }
            while (target[indexT] == '_') {
                ++indexT;
            }

            if (indexS != indexT) {
                if (start[indexS] == 'L' &&
                    indexS < indexT) {  // L 只能向左移动
                    return false;
                }
                if (start[indexS] == 'R' &&
                    indexS > indexT) {  // R 只能向右移动
                    return false;
                }
            }
            ++indexT;
        }

        return true;
    }

private:
    string removeUnderscore(string s) {
        string res;
        for (char ch : s) {
            if (ch != '_') {
                res += ch;
            }
        }
        return res;
    }
};

int main() {
    Solution s;

    string start = "_L__R__R_";
    string start1 = "R_L_";
    string start2 = "_R";

    string target = "L______RR";
    string target1 = "__LR";
    string target2 = "R_";

    assert(s.canChange(start, target) == true);
    assert(s.canChange(start1, target1) == false);
    assert(s.canChange(start2, target2) == false);

    return 0;
}